import { debug } from 'webpack'
import { AppModel } from '../../../../model/AppModel'
import { createPartMenu, createVarMenu } from '../BlockPackage'

declare var Vue : any


export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,


    previousStatement: false ,
    nextStatement: true ,
    hat: true ,
    
    eventHandle: true ,

    init(model:Model, scratch3:any) {
        this.appendDummyInput()
            .appendField("当UDP端口")
        this.appendValueInput("port")
        this.appendDummyInput().appendField("接收到数据")
    } ,

    run (args: any, util: any, runtime: any, model: AppModel) {
    } ,

    coder: {
        JavaScript(block: any, coder: any, model: AppModel) {
            let indent = coder.indentString()
            let port = coder.valueToCode(block, "port", coder.ORDER_ASSIGNMENT)||0
            let code = `${indent}require("besdk/socket").udp.onmessage(${port},async (_pkg_buff)=>{\n`
            
            let nextBlock = block.nextConnection && block.nextConnection.targetBlock()
            if(nextBlock) {
                coder.indent ++
                code+= coder.blockToCode(nextBlock)
                console.log(coder.blockToCode(nextBlock))
                coder.indent --
            }

            code+= `${indent}})\n`
            return code
        }
    }
}
